package com.mlink.web;

import com.mlink.entity.UserEntity;
import com.mlink.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
	private UserMapper userMapper;

    /*
    * 登录
    *
    * */
    @RequestMapping("/login")

    public ModelMap  login(String name,String password) {

         ModelMap modelMap =new ModelMap();
         UserEntity users=userMapper.login(name,password);


         if(users ==null) {
             modelMap.put("result", 0);
         } else {
             modelMap.put("result", 1);
             modelMap.put("id", users.getId());
         }
        return modelMap;


    }

    /*
    *  的到项目所有人员及工时
    *   id   项目id
    * */
    @RequestMapping("/getProjecAllUser")

    public List<UserEntity>  getProjecAllUser(int id) {

        List<UserEntity> users=userMapper.getProjecAllUser(id);

        return users;

    }

    /*
    * 获取所有用户
    *
    * */
	@RequestMapping("/getUsers")

    public List<UserEntity> getUsers() {


        logger.info("logback 成功了");


		List<UserEntity> users=userMapper.getAll();
		return users;
	}
    /*
    * 获取所有用户
    *
    * */
    @RequestMapping("/getOneUser")

    public UserEntity  getUsers(int id) {

        UserEntity users=userMapper.getOne(id);
        return users;
    }

    /*
    * 添加用户
    *
    * */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ModelMap save(@RequestBody UserEntity user) {

        ModelMap mm = new ModelMap();
        List<UserEntity> ues =  userMapper.isUserEixt(user.getUserName());
        if(ues.size() > 0) {
            mm.put("result", 2);// 已存在
        } else {
            int result =  userMapper.insert(user);

            if (result == 0) {
                mm.put("result", 0);//
            } else {
                mm.put("result", 1);//
                mm.put("user", user);// 已存在
            }
        }

         return mm;

    }

    /*
    *更新用户
    *
    * */
    @RequestMapping(value="/updateUser",method = RequestMethod.POST)
    public String update(@RequestBody UserEntity user) {
    	int result =  userMapper.update(user);
        if (result == 1) {
            return "{\"result\":1}";
        } else {
            return "{\"result\":0}";
        }
    }
    
    @RequestMapping(value="/deleteUser" )
    public String delete(int id) {

         int result = userMapper.delete(id);
        if (result == 1) {
            return "{\"result\":1}";
        } else {
            return "{\"result\":0}";
        }
    }


    @RequestMapping(value="/enValid" )
    public void enValid(int id) {
        int  valid  =0;



        userMapper.enValid(valid,id);
    }

}